package com.ibm.mall.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ibm.mall.base.BaseAction;
import com.ibm.mall.model.Appraisal;
import com.ibm.mall.model.CarBean;
import com.ibm.mall.model.Commodity;
import com.ibm.mall.model.OrderForm;
import com.ibm.mall.model.OrderList;
import com.ibm.mall.model.PageBean;
import com.ibm.mall.model.ShippingAddress;
import com.ibm.mall.model.User;
import com.ibm.mall.util.QueryHelper;
import com.ibm.mall.util.SerialNumberUtil;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class CustomerOrderAction extends BaseAction<OrderForm> {

	private static final long serialVersionUID = 1L;
	
	private Integer pageNum;
	
	private Short orderStatus;
	
	private Integer orderId;
	
	private String commodityId;
	
	private String content;
	
	public String mySettle() {
		User user=(User) ActionContext.getContext().getSession().get("user");
		List<ShippingAddress> shippingAddressList=shippingAddressService.getAllShipperAddressByUser(user);
		ActionContext.getContext().put("shippingAddressList", shippingAddressList);
		return "showSettle";
	}
	
	@SuppressWarnings("unchecked")
	public String settle() {
		if(model.getOrderFormPayType()==0){
			SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			User user=(User) ActionContext.getContext().getSession().get("user");
			model.setOrderFormNo(SerialNumberUtil.getOrderNum());
			model.setOrderFormStatus((short) 1);
			model.setInsertTime(sf.format(new Date()));
			model.setStatus((short) 0);
			model.setUser(user);
			Integer id=orderFormService.saveOrder(model);
			model.setId(id);
			Map<Integer,CarBean> car=(Map<Integer,CarBean>)ActionContext.getContext().getSession().get("car");
			for (Entry<Integer, CarBean> entry : car.entrySet()) {
				OrderList orderList=new OrderList();
				orderList.setCommodity(entry.getValue().getCommodity());
				orderList.setInsertTime(sf.format(new Date()));
				orderList.setStatus((short) 0);
				orderList.setOrderListAmount(entry.getValue().getCount());
				orderList.setOrderForm(model);
				orderListService.saveOrderList(orderList);
			}
		}else{
			SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			User user=(User) ActionContext.getContext().getSession().get("user");
			model.setOrderFormNo(SerialNumberUtil.getOrderNum());
			model.setOrderFormStatus((short) 0);
			model.setInsertTime(sf.format(new Date()));
			model.setStatus((short) 0);
			model.setUser(user);
			Integer id=orderFormService.saveOrder(model);
			model.setId(id);
			Map<Integer,CarBean> car=(Map<Integer,CarBean>)ActionContext.getContext().getSession().get("car");
			for (Entry<Integer, CarBean> entry : car.entrySet()) {
				OrderList orderList=new OrderList();
				orderList.setCommodity(entry.getValue().getCommodity());
				orderList.setInsertTime(sf.format(new Date()));
				orderList.setStatus((short) 0);
				orderList.setOrderListAmount(entry.getValue().getCount());
				orderList.setOrderForm(model);
				orderListService.saveOrderList(orderList);
			}
		}
		ActionContext.getContext().getSession().remove("car");
		ActionContext.getContext().getSession().remove("carSize");
		ActionContext.getContext().getSession().remove("totalPrice");
		return "success";
	}
	
	public String myOrder() {
		if(pageNum==null){
			pageNum=1;
		}
		int pageSize=6;
		User user=(User) ActionContext.getContext().getSession().get("user");
		QueryHelper queryHelper=new QueryHelper(OrderForm.class, "o");
		if(orderStatus==null){
			queryHelper.addCondition("o.user=?", user)
			.addCondition("o.status=?", (short)0)
			.addOrderProperty("o.insertTime", false);
		}else{
			queryHelper.addCondition("o.user=?", user)
			.addCondition("o.orderFormStatus=?", orderStatus)
			.addCondition("o.status=?", (short)0)
			.addOrderProperty("o.insertTime", false);
		}
		PageBean orderFormPageBean=orderFormService.getPageBean(pageNum,pageSize,queryHelper);
		ActionContext.getContext().put("orderFormPageBean", orderFormPageBean);
		ActionContext.getContext().put("orderStatus", orderStatus);
		return "showOrder";
	}
	
	public String cancelOrder() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		OrderForm orderForm=orderFormService.getOrderFormById(orderId);
		orderForm.setOrderFormStatus((short) 5);
		orderForm.setLastUpdateTime(sf.format(new Date()));
		orderFormService.updateOrderForm(orderForm);
		return "toShowOrder";
	}
	
	public String confirmOrder() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		OrderForm orderForm=orderFormService.getOrderFormById(orderId);
		orderForm.setOrderFormStatus((short) 3);
		orderForm.setLastUpdateTime(sf.format(new Date()));
		orderFormService.updateOrderForm(orderForm);
		return "toShowOrder";
	}
	
	public String returnOrder() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		OrderForm orderForm=orderFormService.getOrderFormById(orderId);
		orderForm.setOrderFormStatus((short) 6);
		orderForm.setLastUpdateTime(sf.format(new Date()));
		orderFormService.updateOrderForm(orderForm);
		return "toShowOrder";
	}
	
	public String appraisal() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user=(User) ActionContext.getContext().getSession().get("user");
		String[] commodityIds=commodityId.split(",");
		String[] appraisalContents=content.split(",");
		if(commodityIds.length==appraisalContents.length){
			for (int i = 0; i < appraisalContents.length; i++) {
				Commodity commodity=commodityService.getCommodityById(Integer.valueOf(commodityIds[i].trim()));
				Appraisal appraisal=new Appraisal();
				appraisal.setCommodity(commodity);
				appraisal.setAppraisalContent(appraisalContents[i]);
				appraisal.setInsertTime(sf.format(new Date()));
				appraisal.setStatus((short) 0);
				appraisal.setUser(user);
				appraisalService.saveAppraisal(appraisal);
			}
		}
		OrderForm orderForm=orderFormService.getOrderFormById(orderId);
		orderForm.setOrderFormStatus((short) 4);
		orderForm.setLastUpdateTime(sf.format(new Date()));
		orderFormService.updateOrderForm(orderForm);
		return "toShowOrder";
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Short getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(Short orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public String getCommodityId() {
		return commodityId;
	}

	public void setCommodityId(String commodityId) {
		this.commodityId = commodityId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}
